<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        
        .gjzfont li {
            list-style: none;
            font-size: 14px;
            color: #666;
        }
        
        .gjzfont {
            width: 500px;
            margin: 0 auto;
            /* border: 1px solid #333; */
        }
        
        .gjzfont>ul {
            width: 500px;
            height: 192px;
            overflow: hidden;
            border: 1px solid #999;
            display: none;
        }
        
        .gjzfont>.icon {
            width: 494px;
            height: 26px;
            padding: 5px 10px;
            border: 3px solid #e53;
            border-right: none;
            outline: none;
        }
    </style>
</head>

<body>
    <div class="gjzfont">
        <input type="text" class="icon">
        <ul></ul>
    </div>
    <script>
        var icon = document.querySelector('.icon')
        var btn = document.querySelector('button')
        var ul = document.querySelector('.gjzfont>ul')
        var timer
        icon.onkeyup = function() {
            var text = icon.value
            clearInterval(timer)
            timer = setTimeout(() => {

                if (!text) {
                    ul.innerHTML = ''
                    ul.style.display = 'none';
                    return
                }
                var xhr = new XMLHttpRequest()
                xhr.open('get', `/user?age=${text}`, true)
                xhr.send()
                xhr.onreadystatechange = function() {
                    if (xhr.status == 200 && xhr.readyState == 4) {
                        console.log(xhr.responseText);
                        ul.innerHTML = ''

                        var data = JSON.parse(xhr.responseText)
                        if (data.code) {
                            // ul.style.display = 'none';
                            data.data.forEach((item) => {
                                ul.innerHTML += `<li>${item.title}</li>`
                            })
                            if (text) {
                                ul.style.display = 'block';
                            }
                        } else {
                            ul.innerHTML = ''

                        }
                    }
                }
            }, 300)

        }
    </script>
</body>

</html>